package elitech.vietnam.weatherfashion.custom;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.dto.PostDTO;

public class RecentPostAdapter extends ArrayAdapter<PostDTO> {
	
	private OnItemClickListener mListener;

	public RecentPostAdapter(Context context, int textViewResourceId, List<PostDTO> objects) {
		super(context, textViewResourceId, objects);
	}
	
	public void setOnItemClickListener(OnItemClickListener mListener) {
		this.mListener = mListener;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view  = LayoutInflater.from(getContext()).inflate(R.layout.layout_recent_post_item, null);
		ImageView image = (ImageView) view.findViewById(R.id.recent_post_image);
		TextView text = (TextView) view.findViewById(R.id.recent_post_text);
			
		UrlImageViewHelper.setUrlDrawable(image, Const.SERVER_IMAGE_THUMB_URL + getItem(position).getImage(), R.drawable.loading_time);
		text.setText(new SimpleDateFormat("MM/dd").format(getItem(position).getDateShow()));
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mListener != null)
					mListener.onItemClick(getItem(position));
			}
		});
		return view;
	}
	
	public interface OnItemClickListener {
		public void onItemClick(PostDTO item);
	}
}
